package com.app.business.service;

import com.app.business.model.House;
import com.app.system.jpa.JpaBaseDao;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.dataType.IntegerUtils;
import com.app.system.utils.dataType.StringUtils;
import com.google.common.collect.Maps;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Service;

import javax.persistence.Query;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by ddz on 2022/9/21.
 * 公示业务层
 *
 * @author ddz
 */
@SuppressWarnings(value = "ALL")
@Service
public class ShowHjService extends JpaBaseDao<House, Integer> {


    /**
     * 通过征收项目、安置项目、楼号、房源类型\、是否保障区、建筑面积得到所分配的houseList
     *
     * @param zsProjectId
     * @param azProjectId
     * @param buildNum
     * @param houseStyle
     * @param houseHold
     * @param houseArea
     * @return
     */
    public Map<String, List> houseMap(String zsProjectId, Integer azProjectId, String buildNum, String houseHold, String houseStyle, String buildArea, Integer houseStatus) {

        StringBuffer hql = new StringBuffer("from House h where h.azProject.id = :azProjectId ");
        if (StringUtils.isNotEmpty(buildNum)) {
            hql.append(" and h.buildNum in " + buildNum + "  ");
        }
        hql.append(" and h.id in (select zph.houseId from ZsProjectHouse zph where zph.zsProjectId in (" + zsProjectId + ")) ");
        if (StringUtils.isNotEmpty(houseStyle)) {
            hql.append(" and h.houseStyle  in " + houseStyle + " ");
        }
        if (StringUtils.isNotEmpty(houseHold)) {
            hql.append(" and h.houseHold = :houseHold");
        }
        if (StringUtils.isNotEmpty(buildArea)) {
            hql.append(" and h.propertyArea+h.poolArea in " + buildArea + "  ");
        }
        if (houseStatus != null) {
            hql.append(" and h.houseStatus = :houseStatus");
        }
        hql.append(" order by h.houseStyle,h.propertyArea+h.poolArea,h.floor,h.houseNum");
        Map<String, Object> map = Maps.newLinkedHashMap();
        map.put("azProjectId", azProjectId);
        if (StringUtils.isNotEmpty(houseHold)) {
            map.put("houseHold", houseHold);
        }
        if (houseStatus != null) {
            map.put("houseStatus", houseStatus);
        }
        List<House> mapList = this.findHql(hql.toString(), map);
        List houseList = new ArrayList<>();
        Map<String, List> resultMap = Maps.newHashMap();
        if (CollectionUtils.isNotEmpty(mapList)) {
            for (House house : mapList) {
                houseList = resultMap.get(house.getBuildNum()) == null ? new ArrayList() : resultMap.get(house.getBuildNum());
                houseList.add(house);
                resultMap.put(house.getBuildNum(), houseList);
            }
        }
        return resultMap;
    }


    /**
     * 通过征收项目、安置项、房源类型、房源属性、建筑面积得到所分配的houseList
     *
     * @param zsProjectId
     * @param azProjectId
     * @param houseStyle
     * @param houseHold
     * @param houseArea
     * @param houseStatus
     * @return
     */
    public Map<String, List> houseMapFoHjThree(String zsProjectId, Integer azProjectId, String houseHold, String houseStyle, String buildArea, Integer houseStatus) {

        StringBuffer hql = new StringBuffer("from House h where h.azProject.id = :azProjectId  ");
        hql.append(" and h.id in (select zph.houseId from ZsProjectHouse zph where zph.zsProjectId in (" + zsProjectId + ")) ");
        if (StringUtils.isNotEmpty(houseStyle)) {
            hql.append(" and h.houseStyle  in " + houseStyle + " ");
        }
        if (StringUtils.isNotEmpty(houseHold)) {
            hql.append(" and h.houseHold = :houseHold");
        }
        if (StringUtils.isNotEmpty(buildArea)) {
            List<String> areaList = Arrays.asList(buildArea.split(","));
            for (String s : areaList) {
                hql.append(" and h.propertyArea+h.poolArea  " + s + "  ");
            }
        }
        if (houseStatus != null) {
            hql.append(" and h.houseStatus = :houseStatus");
        }
        hql.append(" order by floor(h.propertyArea+h.poolArea),h.buildNum,h.floor,h.houseNum");
        Map<String, Object> map = Maps.newLinkedHashMap();
        map.put("azProjectId", azProjectId);
        if (StringUtils.isNotEmpty(houseHold)) {
            map.put("houseHold", houseHold);
        }
        if (houseStatus != null) {
            map.put("houseStatus", houseStatus);
        }
        List<House> mapList = this.findHql(hql.toString(), map);
        List houseList = new ArrayList<>();
        Map<String, List> resultMap = Maps.newHashMap();
        String areaAndBuild = null;
        if (CollectionUtils.isNotEmpty(mapList)) {
            for (House house : mapList) {
                areaAndBuild = house.getBuildArea().setScale(0, BigDecimal.ROUND_DOWN).toString() + "-" + house.getBuildNum();
                houseList = resultMap.get(areaAndBuild) == null ? new ArrayList() : resultMap.get(areaAndBuild);
                houseList.add(house);
                resultMap.put(areaAndBuild, houseList);
            }
        }
        return resultMap;
    }


    /**
     * 通过征收项目、安置项目、楼号、房源类型\、是否保障区、建筑面积得到所分配的houseNumList
     *
     * @param zsProjectId
     * @param azProjectId
     * @param buildNum
     * @param houseStyle
     * @param houseHold
     * @param houseArea
     * @return
     */
    public List<String> buildNumListFoHjThree(String zsProjectId, Integer azProjectId, String houseHold, String houseStyle, String buildArea, Integer houseStatus) {
        StringBuffer sql = new StringBuffer("select FLOOR(h.property_area+h.pool_area) as area,h.build_num as houseNum from house h where h.az_project_id=:azProjectId ");
        sql.append("  and h.id in (SELECT DISTINCT zph.house_id FROM zs_project_house zph WHERE zph.zs_project_id in (" + zsProjectId + ")) ");
        if (StringUtils.isNotEmpty(houseStyle)) {
            sql.append(" and h.house_style  in " + houseStyle + " ");
        }
        if (StringUtils.isNotEmpty(houseHold)) {
            sql.append(" and h.house_hold = :houseHold");
        }
        if (StringUtils.isNotEmpty(buildArea)) {
            List<String> areaList = Arrays.asList(buildArea.split(","));
            for (String s : areaList) {
                sql.append(" and h.property_Area+h.pool_Area  " + s + "  ");
            }
        }
        if (houseStatus != null) {
            sql.append(" and h.house_Status = :houseStatus");
        }
        sql.append(" GROUP BY FLOOR(h.property_area+h.pool_area),h.build_num ");
        sql.append(" ORDER BY FLOOR(h.property_area+h.pool_area),CONVERT(h.build_num,SIGNED),h.house_num ");

        Query query = this.entityManager.createNativeQuery(sql.toString());
        query.setParameter("azProjectId", azProjectId);
        query.setParameter("houseHold", houseHold);
        if (houseStatus != null) {
            query.setParameter("houseStatus", houseStatus);
        }
        List<Map> mapList = query.unwrap(org.hibernate.SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
        List<String> areaAndBuildList = new ArrayList<>();
        String areaAndBuild = null;
        if (CollectionUtils.isNotEmpty(mapList)) {
            for (Map map : mapList) {
                String area = map.get("area").toString();
                String houseNum = map.get("houseNum").toString();
                areaAndBuild = area + "-" + houseNum;
                areaAndBuildList.add(areaAndBuild);
            }
        }
        return areaAndBuildList;
    }

    /**
     * 通过征收项目、安置项、房源类型、房源属性、建筑面积得到所分配的houseList
     *
     * @param zsProjectId
     * @param azProjectId
     * @param houseStyle
     * @param houseHold
     * @param houseArea
     * @param houseStatus
     * @return
     */
    public Map<String, List> houseMapFoJy(String zsProjectId, Integer azProjectId, String houseHold, String houseStyle, Integer houseStatus) {

        StringBuffer hql = new StringBuffer("from House h where h.azProject.id = :azProjectId  ");
        hql.append(" and h.id in (select zph.houseId from ZsProjectHouse zph where zph.zsProjectId in (" + zsProjectId + ")) ");
        if (StringUtils.isNotEmpty(houseStyle)) {
            hql.append(" and h.houseStyle  in " + houseStyle + " ");
        }
        if (StringUtils.isNotEmpty(houseHold)) {
            hql.append(" and h.houseHold = :houseHold");
        }
        if (houseStatus != null) {
            hql.append(" and h.houseStatus = :houseStatus");
        }

        hql.append(" order by h.buildNum,h.floor,h.houseNum");
        Map<String, Object> map = Maps.newLinkedHashMap();
        map.put("azProjectId", azProjectId);
        if (StringUtils.isNotEmpty(houseHold)) {
            map.put("houseHold", houseHold);
        }
        if (houseStatus != null) {
            map.put("houseStatus", houseStatus);
        }
        List<House> mapList = this.findHql(hql.toString(), map);
        List houseList = new ArrayList<>();
        Map<String, List> resultMap = Maps.newHashMap();
        String buildNum = null;
        if (CollectionUtils.isNotEmpty(mapList)) {
            for (House house : mapList) {
                buildNum = house.getBuildNum();
                houseList = resultMap.get(buildNum) == null ? new ArrayList() : resultMap.get(buildNum);
                houseList.add(house);
                resultMap.put(buildNum, houseList);
            }
        }
        return resultMap;
    }

    /**
     * 通过征收项目、安置项目、楼号、房源类型\、是否保障区、建筑面积得到所分配的houseNumList
     *
     * @param zsProjectId
     * @param azProjectId
     * @param buildNum
     * @param houseStyle
     * @param houseHold
     * @param houseArea
     * @return
     */
    public List<String> buildNumListFoJy(String zsProjectId, Integer azProjectId, String houseHold, String houseStyle, Integer houseStatus, String buildNumStr) {
        StringBuffer sql = new StringBuffer("select FLOOR(h.property_area+h.pool_area) as area,h.build_num as buildNum from house h where h.az_project_id=:azProjectId ");
        sql.append("  and h.id in (SELECT DISTINCT zph.house_id FROM zs_project_house zph WHERE zph.zs_project_id in (" + zsProjectId + ")) ");
        if (StringUtils.isNotEmpty(houseStyle)) {
            sql.append(" and h.house_style  in " + houseStyle + " ");
        }
        if (StringUtils.isNotEmpty(houseHold)) {
            sql.append(" and h.house_hold = :houseHold");
        }
        if (houseStatus != null) {
            sql.append(" and h.house_Status = :houseStatus");
        }
        if (StringUtils.isNotEmpty(buildNumStr)) {
            sql.append(" and h.build_num in " + (buildNumStr) + " ");
        }
        sql.append(" GROUP BY h.build_num ");
        sql.append(" ORDER BY CONVERT(h.build_num,SIGNED),h.house_num ");

        Query query = this.entityManager.createNativeQuery(sql.toString());
        query.setParameter("azProjectId", azProjectId);
        if (StringUtils.isNotEmpty(houseHold)) {
            query.setParameter("houseHold", houseHold);
        }
        if (houseStatus != null) {
            query.setParameter("houseStatus", houseStatus);
        }
        List<Map> mapList = query.unwrap(org.hibernate.SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
        List<String> buildNumList = new ArrayList<>();
        String buildNum = null;
        if (CollectionUtils.isNotEmpty(mapList)) {
            for (Map map : mapList) {
                buildNum = map.get("buildNum").toString();
                buildNumList.add(buildNum);
            }
        }
        return buildNumList;
    }
}
